package com.kly.cashmall.module.products.adapter;

import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kly.cashmall.R;
import com.kly.cashmall.bean.ProductItemBaen;
import com.kly.cashmall.utils.function.GlideUtils;

/**
 * Created by
 * on 2019/4/28.
 */
public class ProductsListAdapter extends BaseQuickAdapter<ProductItemBaen, BaseViewHolder> {

    public ProductsListAdapter() {
        super(R.layout.item_loan_product);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ProductItemBaen item) {
        TextView mItemTitle = holder.getView(R.id.item_title);
        TextView mItemTip = holder.getView(R.id.item_tip);
        TextView mItemAmount = holder.getView(R.id.item_amount);
        TextView mItemConfirm = holder.getView(R.id.item_confirm);
        ProgressBar mItemProgreeBar = holder.getView(R.id.item_progress_bar);
        String icon = item.getCategoryLogo();
        if (icon != null) {
            GlideUtils.loadImage(getContext(),icon,holder.getView(R.id.item_logo));
        }
        mItemTitle.setText(item.getCategoryName());
        mItemTip.setText(item.getCategoryDesc());
        SpannableString string = new SpannableString(item.getCategoryRange());
        if (item.getCategoryRange().contains("₹")) {
            string.setSpan(new RelativeSizeSpan(0.78f), item.getCategoryRange().indexOf("₹"), item.getCategoryRange().indexOf("₹") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        mItemAmount.setText(string);
        mItemProgreeBar.setProgress(item.getCategoryApplicationRatio());
        holder.setText(R.id.item_progress_text, item.getCategoryApplicationRatio() + "%");
        mItemConfirm.setText(item.getButtonName());
        ((TextView) holder.getView(R.id.item_confirm)).setTextColor(item.isApplied() ? getContext().getResources().getColor(R.color.color_black_four) : getContext().getResources().getColor(R.color.color_black_one));
        holder.setVisible(R.id.item_line, (holder.getAdapterPosition() + 1 != getItemCount()));

        Drawable drawableLeft = getContext().getResources().getDrawable(R.mipmap.ic_right_arrow_double);
        if (item.isApplied()) {
            mItemConfirm.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

        } else {
            mItemConfirm.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
        }
    }
}
